/*
 * TelemetryHandlerFactory.java is part of htn-example.
 *
 * TelemetryHandlerFactory.java is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TelemetryHandlerFactory.java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TelemetryHandlerFactory.java.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.htn.telescopes.events;

import ngat.message.GUI_RCS.TELEMETRY_UPDATE;
import ngat.message.GUI_RCS.TelemetryInfo;
import ngat.message.base.COMMAND;
import ngat.net.IConnection;
import ngat.net.camp.CAMPRequestHandler;
import ngat.net.camp.CAMPRequestHandlerFactory;
import ngat.util.logging.LogManager;
import ngat.util.logging.Logger;
import ngat.util.logging.Logging;

/**
 * Creates a new instance of TelemetryHandler for each Telemetry update we receive.
 */
public class TelemetryHandlerFactory implements CAMPRequestHandlerFactory, Logging
{
	public static final String CLASS = "TelemetryHandlerFactory";
	Logger logger = null;
	
	/** Telescope event listener */
	private LPLTelescopeEventListenerImpl eventlistener;

	/**
	 * Constructor. Sets tea. Creates class logger.
	 * @param tea The instance of tea that uses this server.
	 * @see #logger
	 */
	public TelemetryHandlerFactory(LPLTelescopeEventListenerImpl eventlistener)
	{
		logger = LogManager.getLogger("TRACE");
		this.eventlistener = eventlistener;
	}

	/** Selects the appropriate handler for the specified command.
	 * May return <i>null</i> if the ProtocolImpl is not defined or not an
	 * instance of JMSMA_ProtocolServerImpl or the request is not
	 * defined or not an instance of CTRL_TO_RCS.
	 * @see #logger
	 */
	public CAMPRequestHandler createHandler(IConnection connection, COMMAND command)
	{
		logger.log(INFO, 1, CLASS, "tea", "createHandler","Received request: "+command);
       
		// Deal with undefined and illegal args.
		if (connection == null)
		{
			logger.log(INFO, 1, CLASS, "tea", "createHandler","Null connection");
 			return null;
		}
		if ( (command == null) ||
		     ! (command instanceof TELEMETRY_UPDATE) )
		{
			logger.log(INFO, 1, CLASS, "tea", "createHandler","Command "+command+
				   " null or not instance of TELEMETRY_UPDATE.");
			return null;
		}
		// Cast to correct subclass.
		TELEMETRY_UPDATE tu = (TELEMETRY_UPDATE)command;
	
		TelemetryInfo telem = tu.getData();
	
		return new TelemetryHandler(eventlistener,connection, telem); 
	}
}
